/**
 * 客户信息 列表页
 *
 * 作者 ganjun
 * 日期 2024-11-12 14:15:30
 */
layui.use(['jquery', 'laydate', 'febs', 'form', 'table', 'commonJS', 'validate'], function () {
    var $ = layui.jquery,
        laydate = layui.laydate,
        febs = layui.febs,
        form = layui.form,
        table = layui.table,
        commonJS = layui.commonJS,
        validate = layui.validate,
        $view = $('#febs-customer'),
        $query = $view.find('#query'),
        $searchForm = $view.find('#customer-table-form'),
        $statusSearch = $view.find('#statusSearchSelect'),
        tableIns;

    form.verify(validate);
    form.render();

    //状态
    febs.initDictItemSelect($statusSearch, 'CUST_STATUS', null, null);

    initTable();

    function initTable() {
        tableIns = febs.table.init({
            elem: $view.find('table'),
            id: 'customerTable',
            url: ctx + 'customer/list',
            where: getQueryParams(),
            height: getTableHeight(),
            skin: '',
            cols: [[
                // {title: '序号', type: 'numbers',width: 60},
                {field: 'custCode', title: '客户编号', minWidth: 120},
                {field: 'custName', title: '单位名称', minWidth: 120},
                {field: 'contact', title: '联系人', minWidth: 80},
                {field: 'telephone', title: '联系方式', minWidth: 120},
                {field: 'email', title: '邮箱', minWidth: 120},
                {field: 'statusName', title: '状态', minWidth: 60},
                {field: 'createdBy', title: '创建人', minWidth: 80},
                {field: 'createdTime', title: '创建时间', minWidth: 120},
                {title: '操作', toolbar: '#customer-tool', width: 240,align:'center',fixed:'right'}
            ]],
            toolbar: '#customer-toolbar',
            defaultToolbar: []
        });
    }

    table.on('sort(customerTable)', function (obj) {
        tableIns.reload({
            initSort: obj,
            where: $.extend(getQueryParams(), {
                field: obj.field,
                order: obj.type
            }),
            autoSort: true
        });
    });

    function reloadPage(curr) {
        var _curr = $view.find(".layui-laypage-em").next().html();
        if (!!curr) {
            _curr = curr;
        }
        tableIns.reload({where: getQueryParams(), page: {curr: _curr}});
    }

    $query.on('click', function () {
        reloadPage(1);
    });

    table.on('toolbar(customerTable)', function (obj) {
        if (obj.event === 'add') {
            febs.modal.open('添加客户', 'customer/add', {
                btn: ['提交', '取消'],
                area: $(window).width() <= 750 ? ['40%', '60%'] : ['40%', '60%'],
                yes: function (index, layero) {
                    $('#customer-edit').find('#submit').trigger('click');
                },
                btn2: function () {
                    layer.closeAll();
                },
                data: {

                }
            });
        }
        if(obj.event === 'export'){
            var params = getQueryParams();
            febs.download(ctx + 'customer/export', params, '客户信息表.xls');
        }
    });

    table.on('tool(customerTable)', function (obj) {
        var data = obj.data,
        layEvent = obj.event;

        if (layEvent === 'del') {
            febs.modal.confirm('删除数据', '确定删除所选数据？', function () {
                deleteIds(data.id);
            });
        }
        if (layEvent === 'edit') {
            febs.modal.open('修改', 'customer/update/' + data.id, {
                area: $(window).width() <= 750 ? ['40%', '60%'] : ['40%', '60%'],
                btn: ['提交', '取消'],
                yes: function (index, layero) {
                    $('#customer-edit').find('#submit').trigger('click');
                },
                btn2: function () {
                    layer.closeAll();
                },
                data: {

                },
            });
        }

        if (layEvent === 'audit') {
            febs.modal.open('审核', 'customer/audit/' + data.id, {
                area: $(window).width() <= 750 ? ['60%', '60%'] : ['40%', '45%'],
                btn: ['提交', '取消'],
                yes: function (index, layero) {
                    $('#customer-audit').find('#submit').trigger('click');
                },
                btn2: function () {
                    layer.closeAll();
                },
                data: {

                },
            });
        }


        if (layEvent === 'viewReason') {
            febs.modal.open('驳回原因', 'customer/viewReason/' + data.id, {
                area: $(window).width() <= 750 ? ['90%', '90%'] : ['60%', '60%'],
                btn: ['返回'],
                yes: function () {
                    layer.closeAll();
                },
            });
        }
    });

    function getQueryParams() {
        return {
            nameSearch: $searchForm.find('input[name="nameSearch"]').val().trim(),
            contactSearch: $searchForm.find('input[name="contactSearch"]').val().trim(),
            statusSearch: $searchForm.find("select[name='statusSearch']").find("option:selected").val(),
            invalidate_ie_cache: new Date()
        };
    }

    function deleteIds(ids) {
        febs.get(ctx + 'customer/delete/' + ids, null, function () {
            febs.alert.success('删除数据成功');
            $query.trigger('click');
        })
    }

    function getTableHeight() {
        return commonJS.calcTableHeight();
    }

    $(window).resize(function () {
        tableIns.reload({height: getTableHeight()});
    });

});